<!--
 * @Author: weikang
 * @Description:
 * @Date: 2023-12-23 11:38:28
 * @LastEditTime: 2025-01-26 17:22:54
 * @FilePath: /env-manager-pc/src/views/data_query/realtime/index.vue
-->
<script setup lang="ts">
import apiEnt from '@/api/modules/ent'
import { usePage } from '@/utils/commonHook'
import { useEnvOrgs, useOwnerOrgs } from '@/utils/dataHook'
import { QueryObj } from '@/utils/dataHook/data'
import useKeepAliveStore from '@/store/modules/keepAlive'
import useUserStore from '@/store/modules/user'

defineOptions({
  name: 'DataRealTimeList',
})
const keepAliveStore = useKeepAliveStore()

onBeforeRouteLeave((to) => {
  if (to.name === 'data-real-time-detail') {
    keepAliveStore.add('DataRealTimeList')
  }
  else {
    keepAliveStore.remove('DataRealTimeList')
  }
})
const router = useRouter()
const userStore = useUserStore()
const tableData = ref<any[]>([])
const { page, pageNumChange, pageSizeChange } = usePage(getTableData)

const searchData = reactive(new QueryObj({
  objType: 'DIM.Business.Enterprise',
  Filters: ['DIM.Business.Envs.EnterpriseFilters.EnvOrgGUIDFilter', 'DIM.Units.Filters.NameFilter', 'DIM.Units.Filters.OwnerOrgGUIDFilter'],
}))

function search() {
  page.PageNum = 1
  getTableData()
}
const loading = ref(false)
async function getTableData() {
  loading.value = true
  const res = await apiEnt.GetEnterpriseAndDeviceNums({
    objFilter: searchData.getQuery(),
    pageSize: page.PageSize,
    pageNum: page.PageNum,
  })
  loading.value = false
  // console.log('%c [ res ]-27', 'font-size:13px; background:pink; color:#bf2c9f;', res)
  const { Rows, RecordsetCount } = res
  tableData.value = Rows || []
  page.RecordsetCount = RecordsetCount
}

getTableData()

function toDetail(row: any) {
  sessionStorage.setItem('PolluteErrRow', JSON.stringify(row))
  router.push(`/data-query/real-time/electric/${row.EntObjID}`)
}

const OwnerList = userStore.isEnterpris ? [] : useOwnerOrgs()
const envOrgs = userStore.isEnterpris ? [] : useEnvOrgs()
const defaultProps = {
  children: 'Children',
  label: 'Name',
}
</script>

<template>
  <div>
    <PageHeader>
      <template #content>
        <ElForm label-width="100">
          <ElRow>
            <ElCol v-if="!userStore.isEnterpris" :span="6">
              <ElFormItem label="管理机构">
                <el-tree-select
                  v-model="searchData['DIM.Business.Envs.EnterpriseFilters.EnvOrgGUIDFilter']"
                  value-key="Code"
                  :data="envOrgs"
                  check-strictly
                  :render-after-expand="false"
                  :props="defaultProps"
                  clearable
                  class="w-full"
                />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6">
              <ElFormItem label="企业名称">
                <ElInput v-model="searchData['DIM.Units.Filters.NameFilter']" clearable placeholder="请输入" />
              </ElFormItem>
            </ElCol>
            <ElCol v-if="!userStore.isEnterpris" :span="6">
              <ElFormItem label="运维公司">
                <el-tree-select
                  v-model="searchData['DIM.Units.Filters.OwnerOrgGUIDFilter']"
                  value-key="Code"
                  :data="OwnerList"
                  check-strictly
                  :render-after-expand="false"
                  :props="defaultProps"
                  clearable
                  class="w-full"
                />
              </ElFormItem>
            </ElCol>
            <ElCol :span="6" :push="userStore.isEnterpris ? 12 : 0" class="flex flex-justify-end">
              <ElButton type="primary" @click="search">
                查询
              </ElButton>
            </ElCol>
          </ElRow>
        </ElForm>
      </template>
    </PageHeader>
    <PageMain>
      <div v-auto-height="85">
        <vxe-table
          border
          height="100%"
          :data="tableData"
          :loading="loading"
          :row-config="{ isCurrent: true }"
        >
          <VxeColumn type="seq" title="序号" width="80" />
          <VxeColumn field="EnvOrgName" title="管理机构" min-width="150" show-overflow />
          <VxeColumn field="OwnerOrgName" title="运维公司" min-width="180" show-overflow />
          <VxeColumn field="EntName" title="企业名称" min-width="220" show-overflow />
          <!-- <el-table-column prop="date" label="安装时间" />
          <el-table-column prop="date" label="安装日期" /> -->
          <VxeColumn field="EntAllDevNum" title="监测设备数" width="120" />
          <VxeColumn title="操作" fixed="right" width="120">
            <template #default="{ row }">
              <ElButton type="primary" link @click="toDetail(row)">
                实时数据
              </ElButton>
            </template>
          </VxeColumn>
        </vxe-table>
      </div>
      <ElPagination
        class="mt-4"
        :page-size="page.PageSize"
        :total="page.RecordsetCount"
        :current-page="page.PageNum"
        layout="total, sizes, prev, pager, next, jumper"
        :page-sizes="[20, 50, 100]"
        @current-change="pageNumChange"
        @size-change="pageSizeChange"
      />
    </PageMain>
  </div>
</template>
